package com.cheegu.common.parser.gpsdata.pojo;

import com.cheegu.common.parser.gpsdata.Unpacker;
import com.cheegu.common.parser.gpsdata.unpacker.DefaultUnpacker;
import com.cheegu.common.parser.gpsdata.util.CodingUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 说明
 *
 * @author: zhangxh
 * @email: zhangxh@cheegu.com
 * @date: 2018/3/22 17:19
 */
public class BSJA5CLocation extends GpsDataEntity {
    @Override
    public int ReadBuffer(int pos, byte[] buffer) {

        try {
            Unpacker unpacker = new DefaultUnpacker(buffer);
            byte[] dataBuffer = unpacker.readBytes(6);
            String sTime = "20" + CodingUtils.bytesToHexString(dataBuffer);
            reportTime = CodingUtils.formatStringToDate(sTime,"yyyyMMddhhmmss");
            localTime=new Date();
            ReceiveTime=new Date();
            int LatFlag = 1;
            dataBuffer = unpacker.readBytes(4);
            if ((dataBuffer[0]&0x80)==0x80){
                LatFlag=-1;
            }
            //去掉最高位
            dataBuffer[0] = (byte)(dataBuffer[0] & 0x7F);
            String strValue = CodingUtils.bytesToHexString(dataBuffer);
            //度
           int degree= Integer.parseInt(strValue.substring(1, 2));
            //分
            String tmp = strValue.substring(3);
            double minute =Double.parseDouble(tmp)/ 1000;
            OLat = (degree + minute / 60) * LatFlag;

            int LngFlag = 1;
            dataBuffer = unpacker.readBytes(4);
            if ((dataBuffer[0] & 0x80) == 0x80)
            {
                //西经
                LngFlag = -1;
            }
            //去掉最高位
            dataBuffer[0] = (byte)(dataBuffer[0] & 0x7F);
            strValue = CodingUtils.bytesToHexString(dataBuffer);
            //度
            degree =Integer.parseInt(strValue.substring(0, 3));
            //分
            tmp = strValue.substring(3);
            minute =Double.parseDouble(tmp) / 1000;
            OLng = (degree + minute / 60) * LngFlag;

            //速度
            dataBuffer = unpacker.readBytes(2);
            strValue = CodingUtils.bytesToHexString(dataBuffer);
            Speed = Short.parseShort(strValue) * 10;


            //方向
            dataBuffer = unpacker.readBytes(2);
            strValue = CodingUtils.bytesToHexString(dataBuffer);
            Orientation =Short.parseShort(strValue);

            //定位状态
            byte Status = unpacker.readByte();
            //定位状态
            if ((Status & 0x80) == 0x80)
            {
                IsValid = true;
            }
            else
            {
                IsValid = false;
            }

            //天线状态
            if ((Status & 0x20) == 0x20)
            {
                if ((Status & 0x40) == 0x40)
                {
                    //GPS天线正常
                    AntennaState = false;

                }
                else
                {
                    //GPS天线断开
                    AntennaState = true                    ;
                }
            }
            //电源状态
            byte b = (byte)(Status & 0x18);
            if (b == 0x18)
            {
                this.PowerState = false;
            }
            if (b == 0x10)
            {
                this.PowerState = true;
                this.VehicleAlarm.TerminalMainPowerCut = 1;
                this.VehicleAlarm.PowerCutAlarm = 1;
            }
            if (b == 0x8)
            {
               this.VehicleAlarm.LowVoltage = 1;
                this.VehicleAlarm.TerminalMainPowerLow=1;
            }
            unpacker.readBytes(3);
            //车辆状态4字节 4bytes
            dataBuffer=unpacker.readBytes(4);
//第二个字节的 D3=1为终端光感拆除
            if ((dataBuffer[1] & 0x08) != 0)
            {
                this.VehicleAlarm.MachineBackout = 1; //暂时先读取解析，有再看扩展位
            }
            //跳过8个保留字节
            unpacker.readBytes(8);
            //减后固定的34字节
            int ExtendLength = buffer.length - 34;
            //扩展数据
            byte[] ExtendBuffer = unpacker.readBytes(ExtendLength);
            Unpacker unpack=new DefaultUnpacker(ExtendBuffer);
            List<ExtendData> ExtendDataList=new LinkedList<>();


        } catch (Exception ex) {

        }

        // String sTim="20"+
        return 0;
    }
}
